Thread: while loop reading spaces as '\0' ?

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    182

    while loop reading spaces as '\0' ?

    I'm trying to make setArray remove spaces and punctuation from my array. But it seems like as soon as it encounters a space the while loop is exiting out. Is a blank the space the same as '\o' ?

    I'm using "a man a plan canal panama" as my palindrome.

    Code:
    #include <stdio.h>
    
    int testPalindrome(char[], int, int);
    void setArray(char[]);
    
    int main()
    {
       char string[ 25 ];
       int arraySize = 15, nSize = 15;
       scanf("&#37;s", &string);
    
       setArray( string );
       /* Test Statement */
       printf("%s\n", string);
    
       while( string[ arraySize - 1 ] != '\0' ) {
          --arraySize;
          printf("arraySize: %d\n", arraySize);
       }
    
       --arraySize;
       nSize = arraySize - 1;
    
       printf("arraySize: %d nSize: %d\n", arraySize, nSize);
       printf("%d\n", testPalindrome(string, nSize, arraySize) );
    
    
    return 0;
    }
    
    
    
    void setArray(char theArray[])
    {
       int n = 0;
       int i;
    
       while (theArray[ n ] != '\0') {
          if (theArray[ n ] == ' ' || theArray[ n ] == '.' || theArray[ n ] == '?') {
             i = n;
             while (theArray[ i ] != '\0' ) {
                theArray[ i ] = theArray[ i + 1 ];
                ++i;
             }
             theArray[ i ] = theArray[ i + 1 ];
          }
          ++n;      
       }
    
       return;
    }
    
    
    
    int testPalindrome(char array[], int n, int size)
    {
       if (size % 2 == 1 && (size - 1) / 2 == n ) {
          // TEST STATEMENT
          printf("if 1 n: %d\n", n);
          return 1;
       }
       else if (size % 2 == 0 && (size - 1) / 2 == n) {
          // TEST STATEMENT
          printf("if 2 n: %d\n", n);
          if (array[ n ] == array[ size - n ])
             return 1;
          else
             return 0;
       }
       else {
          // TEST STATEMENT
          printf("if 3 n: %d\n", n);
          if (array[ n ] == array[ size - n - 1 ])
             return testPalindrome(array, n - 1, size);
          else
             return 0;
       }
    
    }
    Last edited by yougene; 09-09-2007 at 05:47 PM.

  2. #2
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    477
    No, blank space is not equal to 0. Space is 0x20 and 0 is just.. 0.

    Anyway, your code works fine for me. I simply C/P the setArray function and tried it, worked as intended.

    Sure that's the problem?

    EDIT:

    Oh, silly me. You're using scanf, which doesn't handle spaces very well.

    Use fgets for strings with spaces in them. Or no, just use fgets for all strings.

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Stop doing this.
    Code:
       while( string[ arraySize - 1 ] != '\0' ) {
          --arraySize;
          printf("arraySize: %d\n", arraySize);
       }
    Go forward. Don't overthink it.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    182
    --> Dave Sinkula
    What? stop doing what?

    --> Ice Dane
    Problem is my book hasn't taught me anything about fgets, so either I have a poorly written inconsistent book, or I'm missing something .

    Try inputting "a man a plan canal panama" as the palindrome.

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Right here you likely encounter undefined behavior:
    Code:
    string[ arraySize - 1 ]
    After which the rest of your program does not matter.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Registered User
    Join Date
    May 2006
    Posts
    182
    I see, the thing is I had the program working without setArray before, and the program still works fine when I say right ra.dar instead of radar.

    I'm going to try replacing that with strlen() as someone suggested in the other thread, but I don't see how it can be the problem in this situation.

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    You're still making the same mistakes you made in the other two threads.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading Text File (spaces problem)
    By kekewong in forum C Programming
    Replies: 1
    Last Post: 04-15-2009, 03:34 PM
  2. Changing 3 spaces to tabs
    By dnguyen1022 in forum C Programming
    Replies: 2
    Last Post: 12-22-2008, 12:51 AM
  3. Problem with a loop reading lines of a file
    By PAragonxd in forum C++ Programming
    Replies: 5
    Last Post: 09-14-2008, 12:57 AM
  4. loop needed also how to make input use letters
    By LoRdHSV1991 in forum C Programming
    Replies: 3
    Last Post: 01-13-2006, 05:39 AM
  5. I need help as soon as possible.
    By hyrule in forum C++ Programming
    Replies: 7
    Last Post: 11-09-2005, 05:49 PM